package com.breakfast.fun.ui.home.product;

import com.breakfast.fun.data.entity.home.product.ProductDetail;
import com.breakfast.fun.data.entity.home.product.ProductState;
import com.breakfast.fun.util.Api;
import com.breakfast.fun.util.L;
import com.breakfast.fun.util.T;

import java.util.Map;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2016/9/6.
 */
public class NormalProductPresenter extends NormalProductContract.Presenter {
    @Override
    public void getProductState() {
        Map<String, String> map = Api.getInstance().getBaseParams();

        map.put("action", "weekday");


        rxManager.add(model.getProductStateModel(map)
                .subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Subscriber<ProductState>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        T.show("获取数据失败");
                    }

                    @Override
                    public void onNext(ProductState productState) {
                        if (productState.isSuccess()) {
                            view.showProductState(productState);
                        } else {
                            T.show(productState.getMsg());
                        }
                    }
                }));
    }

    @Override
    public void getProductDetail(String classId) {
        Map<String, String> map = Api.getInstance().getBaseParams();

        map.put("action", "productType");
        map.put("begin", "0");
        map.put("end", "100");
        map.put("classId", classId);

        L.d("classId----" + classId);
        showLoadingDialog(true);

        rxManager.add(model.getProductDetailModel(map)
                .subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Subscriber<ProductDetail>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        T.show("获取数据失败");
                        hideLoadingDialog();
                    }

                    @Override
                    public void onNext(ProductDetail productDetail) {
                        if (productDetail.isSuccess()) {
                            view.showProductDetail(productDetail);
                        } else {
                            T.show(productDetail.getMsg());
                        }
                        hideLoadingDialog();
                    }
                }));
    }

    @Override
    public void onStart() {
        getProductState();
    }
}
